﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Credera.Theta.API;

namespace Credera.Theta.Modules.Environments.Commands
{
    [Command("DeleteEnvironment")]
    [Synopsis("Permenantly deletes an environment - this cannot be undone")]
    class DeleteEnvironmentCommand : InputCommand
    {
        [Parameter(ordinalPosition:1, autoCompleteValuesFunction:"GetEnvironmentNameAutoComplete", useAutoCompleteForValidation:true)]
        [Synopsis("The name of the environment to delete")]
        public string Name { get; set; }

        [Switch]
        [Synopsis("Whether or not to force the deletion of the environment")]
        public SwitchParameter Force { get; set; }

        public override void Invoke(CommandInvocationContext context)
        {
            if (EnvironmentLogic.Instance.EnvironmentsCollection.Environments.Exists(e => e.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
            {
                if (EnvironmentLogic.Instance.LoadedEnvironments.Exists(e => e.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase))
                    && !this.Force.IsPresent)
                {
                    base.Out.Warning.WriteLine("The environment is loaded. Unload the environment, or use the 'Force' switch to unload the environment before removing.");
                }
                else
                {
                    if (base.Confirm("The environment along with all associated modules and contexts will be deleted. This cannot be undone.", Component.ConfirmationResult.No))
                    {
                        var env = EnvironmentLogic.Instance.EnvironmentsCollection.Environments.FirstOrDefault(e => e.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase));

                        if (EnvironmentLogic.Instance.LoadedEnvironments.Exists(e => e.Name.Equals(this.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            EnvironmentLogic.Instance.Unload(env, base.Host.Modules, base.Out);
                            base.Out.Standard.WriteLine("Environment unloaded.");
                        }

                        EnvironmentLogic.Instance.Remove(env, base.Host.Modules, base.Out);
                        EnvironmentLogic.Instance.Save();

                        base.Out.Standard.WriteLine("Environment removed.");
                    }
                    else
                    {
                        base.Out.Standard.WriteLine("The environment has NOT been deleted.");
                    }
                }
            }
            else
            {
                base.Out.Standard.WriteLine("Environment could not be found.");
            }
        }

        public void GetEnvironmentNameAutoComplete(AutoCompleteValuesFunctionContext context)
        {
            foreach (var environment in EnvironmentLogic.Instance.EnvironmentsCollection.Environments)
                context.Values.Add(environment.Name);
        }
    }
}
